package com.ctg.behavior.calc.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.io.FileUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CtFileUtils {

	public static List<Long> listFileNumbers(String dir) {
		List<Long> fileNumbers = new ArrayList<Long>();

		String[] extensions = new String[] { "json", "txt" };
		Collection<File> funnelFiles = FileUtils.listFiles(new File(dir), extensions, false);
		for (File file : funnelFiles) {
			String name = file.getName();
			log.info("file name={}", name);
			try {
				int index = name.lastIndexOf('.');
				String numberName = name.substring(0, index);
				Long fileNO = Long.parseLong(numberName);
				fileNumbers.add(fileNO);
			} catch (Exception e) {
				log.error("file path={}", file.getPath());
			}
			if (fileNumbers.size() >= 1000) {
				break;
			}
		}

		// 从小到大排序
		fileNumbers.sort(new Comparator<Long>() {
			@Override
			public int compare(Long o1, Long o2) {
				if (o1 < o2) {
					return -1;
				} else if (o1 == o2) {
					return 0;
				} else {
					return 1;
				}
			}
		});

		return fileNumbers;
	}

}
